#!/bin/bash

function memory() {
    local mem_total=$(cat /proc/meminfo|awk '/MemTotal/{print $2}')
    local mem_free=$(cat /proc/meminfo|awk '/MemFree/{print $2}')
    local buffers=$(cat /proc/meminfo|awk '/Buffers/{print $2}')
	local cached=$(cat /proc/meminfo|awk '/^Cached/{print $2}')
    local mem_used=$(expr $mem_total - $mem_free - $buffers - $cached)
	# echo "--------memory-usage-------"
	# echo "  used   buff   cach   free"
	# echo " -----  -----  -----  -----"
	awk 'BEGIN{printf("%6.0fM%6.0fM%8.0fM%8.0fM\n","'$mem_used'"/1024.0 ,"'$buffers'"/1024.0,"'$cached'"/1024.0,"'$mem_free'"/1024.0)}'
        #echo "Mem: $mem_total kB total, $mem_used kB used, $mem_free kB free, $buffers kB buffers, $cached kB cached "
}

function swap() {
	local swap_total=$(cat /proc/meminfo|awk '/SwapTotal/{print $2}')
	local swap_free=$(cat /proc/meminfo|awk '/SwapFree/{print $2}')
	local swap_used=$(expr $swap_total - $swap_free)
	local swap_cached=$(cat /proc/meminfo|awk '/SwapCached/{print $2}')
	# echo "-------swap------"
	# echo " used  free  cach"
	# echo "-----  ----  ----"
	awk 'BEGIN{printf("%5.1fM %7.1fM %6.1fM\n","'$swap_used'"/1024.0,"'$swap_free'"/1024.0,"'$swap_cached'"/1024.0)}'
	#echo "Swap: $swap_total kB total, $swap_used kB used, $swap_free kB free, $swap_cached kB cached"
}

function cpu() {
	local max_cpu_index=$(expr $(cat /proc/stat |awk '$1~/cpu/{print NR}'|tail -1) - 2)
	if [[ -n $1 ]]; then
		if (( $1 > $max_cpu_index ))
		then
			echo "None of the stats you selected are available." >&2
			exit 1
		fi		
	fi


	
	local cpu_name="cpu$1"
	
	#echo "----total-${cpu_name}-usage----"
	#echo "usr sys idl wai hiq siq"


	local cpu_info=$(cat /proc/stat |grep $(echo $cpu_name)|awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	local total_cpu_time=$(echo $cpu_info|awk '{print $1+$3+$4+$5+$6+$7}')
	local user=$(echo $cpu_info |awk '{print $1}')
	#local nice=$(echo $cpu_info |awk '{print $2}')
	local system=$(echo $cpu_info |awk '{print $3}')
	local idle=$(echo $cpu_info |awk '{print $4}')
	local iowait=$(echo $cpu_info |awk '{print $5}')
	local irq=$(echo $cpu_info |awk '{print $6}')
	local softirq=$(echo $cpu_info |awk '{print $7}')

	local user_rate=$(awk 'BEGIN{printf("%0.1f"),("'$user'"-"'$g_user'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	local system_rate=$(awk 'BEGIN{printf("%0.1f"),("'$system'"-"'$g_system'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	local idle_rate=$(awk 'BEGIN{printf("%0.1f"),("'$idle'"-"'$g_idle'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	local iowait_rate=$(awk 'BEGIN{printf("%0.1f"),("'$iowait'"-"'$g_iowait'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	local irq_rate=$(awk 'BEGIN{printf("%0.1f"),("'$irq'"-"'$g_irq'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	local softirq_rate=$(awk 'BEGIN{printf("%0.1f"),("'$softirq'"-"'$g_softirq'")/("'$total_cpu_time'"-"'$g_total_cpu_time'")*100}')
	g_total_cpu_time=$total_cpu_time
	g_user=$user
	g_system=$system
	g_idle=$idle
	g_iowait=$iowait
	g_irq=$irq
	g_softirq=$softirq
	#echo "$user_rate $system_rate $idle_rate $iowait_rate $irq_rate $softirq_rate"
	printf "%4.1f%5.1f%6.1f%5.1f%5.1f%5.1f\n" $user_rate $system_rate $idle_rate $iowait_rate $irq_rate $softirq_rate
}

#将一个流量转换为合适的值，如10240B，输出为10M
#输入参数：一个字节流量
#输出：一个合适的流量值
function transfer() {
	local result="$1"
	local compare=$(awk -v n1=$result -v n2=1024 'BEGIN{print (n1<n2)?1:0}')
	if [[ $compare == 1 ]]; then
		echo "${result}B"
	else
		result=$(awk 'BEGIN{printf("%0.1f"),"'$result'"/1024.0}')
		compare=$(awk -v n1=$result -v n2=1024 'BEGIN{print (n1<n2)?1:0}')
		if [[ $compare == 1 ]]; then
			echo "${result}K"
		else
			result=$(awk 'BEGIN{printf("%0.1f"),"'$result'"/1024.0}')
			compare=$(awk -v n1=$result -v n2=1024 'BEGIN{print (n1<n2)?1:0}')
			if [[ $compare == 1 ]]; then
				echo "${result}M"
			else
				result=$(awk 'BEGIN{printf("%0.1f"),"'$result'"/1024.0}')
				echo "${result}G"
			fi
		fi
	fi
}


#监控网卡的实时流量，在init_network中已经对网卡的参数有效性做了判断，这里就不需要再判断了
function network(){
	local net_name="eth$1"
	#网卡没有被使用的时候（如这里的eth1），域的分隔会出现不同，本来eth0:62682526整个是第一个域，eth1的第一个域为eth1,不包括后面的0
	#所以这里要做特殊处理，这里bytes和packets之间有一定联系，只要一个为0，另一个一定为0，反之一个不为0，另一个也一定不为0
# Inter-|   Receive                                                |  Transmit
#  face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
#     lo:109661817  128680    0    0    0     0          0         0 109661817  128680    0    0    0     0       0          0
#   eth0:62682526  279907    0  164    0     0          0       700  4337654   21114    0    0    0     0       0          0
#   eth1:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0

	
	local net_info=$(cat /proc/net/dev|grep $(echo $net_name)|awk '{print $1,$2,$3,$9,$10,$11}')
	
	local tmp=$(echo $net_info|awk '{print $1}')
	if [[ ${#tmp} == 5 ]]; then
		#statements
		local recv_bytes=$(echo $net_info|awk '{print $2}')
		local recv_packages=$(echo $net_info|awk '{print $3}')
		local send_bytes=$(echo $net_info|awk '{print $5}')
		local send_packages=$(echo $net_info|awk '{print $6}')
	else
		local recv_bytes=$(echo $net_info|awk '{print $1}'|awk -F: '{print $2}')
		local recv_packages=$(echo $net_info|awk '{print $2}')
		local send_bytes=$(echo $net_info|awk '{print $4}')
		local send_packages=$(echo $net_info|awk '{print $5}')
	fi

	local interval_recv_bytes=$(expr $recv_bytes - $g_recv_bytes)
	local interval_recv_packages=$(expr $recv_packages - $g_recv_packages)
	local interval_send_bytes=$(expr $send_bytes - $g_send_bytes)
	local interval_send_packages=$(expr $send_packages - $g_send_packages)

	g_recv_bytes=$recv_bytes
	g_recv_packages=$recv_packages
	g_send_bytes=$send_bytes
	g_send_packages=$send_packages

	local recv_bytes_result=$(transfer $interval_recv_bytes)

	local send_bytes_result=$(transfer $interval_send_bytes)


	#echo "${interval_recv_bytes}B  ${interval_send_bytes}B   $interval_recv_packages    $interval_send_packages"
	printf "%7s%7s%7s%7s\n" $recv_bytes_result $send_bytes_result $interval_recv_packages $interval_send_packages

}

#计算系统负载
function load_avg() {
	local load_info=$(cat /proc/loadavg|awk '{print $1,$2,$3}')
	local one_min=$(echo $load_info|awk '{printf("%0.2f"),$1}')
	local five_min=$(echo $load_info|awk '{printf("%0.2f"),$2}')
	local fif_min=$(echo $load_info|awk '{printf("%0.2f"),$3}')
	echo "$one_min $five_min $fif_min"
}

#计算页面置换的pagein和pageout
#注意：跟dstat的结果有不一样，dstat用pagein和pageout一直是0，很奇怪
function page() {
	#set -x
	local page_info=$(cat /proc/vmstat|awk 'BEGIN{ORS=" "};/^pgpgin/{print $2};/^pgpgout/{print $2}')
	local pagein=$(echo $page_info|awk '{print $1}')
	local pageout=$(echo $page_info|awk '{print $2}')
	local interval_pagein=$(expr $pagein - $g_pagein)
	local interval_pageout=$(expr $pageout - $g_pageout)
	echo " $interval_pagein  $interval_pageout"
	g_pagein=$pagein
	g_pageout=$pageout
	#set +x
}

#initialize the basis cpu info to calculate the rate of user,idle,system etc...
#根据传进来的cpu序号，判断是否存在，然后初始化一些基本信息用于计算cpu的使用率
function init_cpu() {
	local max_cpu_index=$(expr $(cat /proc/stat |awk '$1~/cpu/{print NR}'|tail -1) - 2)
	if [[ -n $1 ]]; then
		if (( $1 > $max_cpu_index ));then
			echo "None of the stats you selected are available." >&2
			exit 1
		fi
	fi
    
    local cpu_name="cpu$1"
	g_cpu_info=$(cat /proc/stat |grep $(echo $cpu_name)|awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	g_total_cpu_time=$(echo $g_cpu_info|awk '{print $1+$3+$4+$5+$6+$7}')
	g_user=$(echo $g_cpu_info | awk '{print $1}')
	g_system=$(echo $g_cpu_info | awk '{print $3}')
	g_idle=$(echo $g_cpu_info | awk '{print $4}')
	g_iowait=$(echo $g_cpu_info | awk '{print $5}')
	g_irq=$(echo $g_cpu_info |awk '{print $6}')
	g_softirq=$(echo $g_cpu_info |awk '{print $7}')
}


#初始化网卡的一些信息
function init_network() {
	local max_net_index=$(cat /proc/net/dev|awk '/eth/{count++};END{print count-1}')
	if (( $1 > $max_net_index )); then
		#statements
		echo "None of the NIC stats you selected are available" >&2
		exit 1
	fi
	local net_name="eth$1"
	g_net_info=$(cat /proc/net/dev|grep $(echo $net_name)|awk '{print $1,$2,$3,$9,$10,$11}')

	local tmp=$(echo $g_net_info|awk '{print $1}')
	if [[ ${#tmp} == 5 ]]; then
		#statements
		g_recv_bytes=$(echo $g_net_info|awk '{print $2}')
		g_recv_packages=$(echo $g_net_info|awk '{print $3}')
		g_send_bytes=$(echo $g_net_info|awk '{print $5}')
		g_send_packages=$(echo $g_net_info|awk '{print $6}')
	else
		g_recv_bytes=$(echo $g_net_info|awk '{print $1}'|awk -F: '{print $2}')
		g_recv_packages=$(echo $g_net_info|awk '{print $2}')
		g_send_bytes=$(echo $g_net_info|awk '{print $4}')
		g_send_packages=$(echo $g_net_info|awk '{print $5}')
	fi	
}

#初始化进程的创建的信息
function init_process(){
	g_processes=$(cat /proc/stat|awk '/^processes/{print $2}')
}

function process(){
	local run=$(cat /proc/stat|awk '/^procs_running/{print $2}')
	local block=$(cat /proc/stat|awk '/^procs_blocked/{print $2}')
	local processes=$(cat /proc/stat|awk '/^processes/{print $2}')
	local new=$(expr $processes - $g_processes)
	g_processes=$processes
	printf "%3d%4d%4d\n" $run $block $new
}

#判刑一个参数选项是否在参数列表中
#需要传入两个参数，第一个参数是某个参数选项，第二个参数为参数列表
#如果参数在参数列表中，返回它的下标，否则返回0
#注意：直接用$2不能得到第二个参数列表，而是参数列表中的第一个元素
function is_exist() {
	local index=1
	arg_list=$(echo "$*"|cut -d " " -f 2-$#)
	for arg in $arg_list; do
		if [[ $1 == $arg ]]; then
			return $index
		fi
		index=$(expr $index + 1)
	done
	return 0
}

#初始化页面置换信息
function init_page() {
	local g_page_info=$(cat /proc/vmstat|awk 'BEGIN{ORS=" "};/^pgpgin/{print $2};/^pgpgout/{print $2}')
	g_pagein=$(echo $g_page_info|awk '{print $1}')
	g_pageout=$(echo $g_page_info|awk '{print $2}')
}

#根据某个参数在参数列表中的索引，从参数列表中获取它的值
#第一个参数为参数选项在列表中的索引，第二个参数为参数列表
#返回参数选项的值,返回值是个数字
function get_num_value() {
	local index=$1
	for((i=0;i<=index;i++))
	do
		shift
	done
	return_value=$1

	#判断return_value是否是个数字
	return_value=$(echo $return_value|awk '{print ($0~/[[:digit:]]/)?"'$return_value'":""}')
}

#根据某个参数在参数列表中的索引，从参数列表中获取它的值
#第一个参数为参数选项在列表中的索引，第二个参数为参数列表
#返回参数选项的值,返回值是字符串
function get_str_value() {
	local index=$1
		for((i=0;i<=index;i++))
	do
		shift
	done
	str_return_value=$1
}

#初始化磁盘读写信息
#输入参数：哪块磁盘，如hda，sda等
function init_disk() {
	if [[ -z $(cat /proc/diskstats|grep "\<$1\>") ]]; then
		#statements
		echo "No suitable block devices found to monitor"
		exit 1
	fi
	#获得扇区的大小
	sector=$(fdisk -l|grep -A 2 "\<$1\>"|tail -n 1|awk '{print $7}')
	#sector=$(fdisk -l|awk '/^Units/{print $7}')
	#第6和第10个域分别是读扇区总数和写扇区总数
	local g_disk_info=$(cat /proc/diskstats|grep -E "\<$1\>"|awk '{print $6,$10}')
	g_disk_read=$(echo $g_disk_info|awk '{print $1}')
	g_disk_write=$(echo $g_disk_info|awk '{print $2}')

}

#计算磁盘的io读写
function disk_io() {
	local disk_info=$(cat /proc/diskstats|grep -E "\<$1\>"|awk '{print $6,$10}')
	local disk_read=$(echo $disk_info|awk '{print $1}')
	local disk_write=$(echo $disk_info|awk '{print $2}')
	local interval_disk_read=$(expr $disk_read - $g_disk_read)
	local interval_disk_write=$(expr $disk_write - $g_disk_write)

	#echo " $interval_disk_read  $interval_disk_write"
	#echo $sector
	local interval_read_byte=$(expr $interval_disk_read \* $sector)
	local interval_write_byte=$(expr $interval_disk_write \* $sector)
	if (( $interval_read_byte > 1024 )); then
		awk 'BEGIN{printf("%2.0fk"),"'$interval_read_byte'"/1024.0}'
	else
		printf "%2.0fb" $interval_read_byte
	fi
	if (( $interval_write_byte > 1024 )); then
		awk 'BEGIN{printf("%6.0fk\n"),"'$interval_write_byte'"/1024.0}'
	else
		printf "%6.0fb\n" $interval_write_byte
	fi
	g_disk_read=$disk_read
	g_disk_write=$disk_write
}

#计算总的磁盘读写
function total_disk_io() {
	count=0
	local total_disk_io_result=()
	for disk in $all_disk_info; do
		#statements
		local disk_info=$(cat /proc/diskstats|grep -E "\<$disk\>"|awk '{print $6,$10}')
		local disk_read=$(echo $disk_info|awk '{print $1}')
		local disk_write=$(echo $disk_info|awk '{print $2}')

		local interval_disk_read=$(expr $disk_read - ${all_disk_io_info[$count]})
		#更新一下all_disk_io_info数组
		all_disk_io_info[$count]=$disk_read

		count=$(expr $count + 1)
		local interval_disk_write=$(expr $disk_write - ${all_disk_io_info[$count]})
		all_disk_io_info[$count]=$disk_write

		#echo " $interval_disk_read  $interval_disk_write"
		#echo $sector
		local sector_index=$(((count+1)/2-1))
		local interval_read_byte=$(expr $interval_disk_read \* ${all_disk_sector_info[$sector_index]})
		local interval_write_byte=$(expr $interval_disk_write \* ${all_disk_sector_info[$sector_index]})
		total_disk_io_result=(${total_disk_io_result[*]} $interval_read_byte $interval_write_byte)
		count=$(expr $count + 1)
	done
	#当退出循环的时候，total_disk_io_result数组中偶数位的和是磁盘总read信息，奇数位的和为磁盘总write信息
	local total_read=0
	local total_write=0
	for (( i = 0; i < ${#total_disk_io_result[*]}; i++ )); do
		last=$(echo ${i:$((${#i}-1))})	#取i的最后一位
		if [[ $last =~ [02468] ]]; then
			total_read=$(expr $total_read + ${total_disk_io_result[$i]})
		else
			total_write=$(expr $total_write + ${total_disk_io_result[$i]})
		fi
	done

	#输出磁盘总的读写值
	if (( $total_read > 1024 )); then
		awk 'BEGIN{printf("%2.0fk"),"'$total_read'"/1024.0}'
	else
		printf "%2.0fb" $total_read
	fi
	if (( $total_write > 1024 )); then
		awk 'BEGIN{printf("%6.0fk\n"),"'$total_write'"/1024.0}'
	else
		printf "%6.0fb\n" $total_write
	fi
}

function init_total_disk() {
	all_disk_info=$(fdisk -l|awk '/^Disk/{print $2}'|awk -F"/" '{print $3}'|awk -F":" '{print $1}')
	all_disk_io_info=()
	all_disk_sector_info=()
	for disk in $all_disk_info; do
		#statements
		init_disk $disk
		all_disk_io_info=(${all_disk_io_info[*]} $g_disk_read $g_disk_write)
		all_disk_sector_info=(${all_disk_sector_info[*]} $sector)
		#echo ${all_disk_io_info[*]}
		#echo ${all_disk_sector_info[*]}
	done
	#echo ${all_disk_io_info[*]}
	#echo ${all_disk_sector_info[*]}
}

#显示topN-cpu以及他们的事件
function topN_cpu() {
	local mysql_info="$(/opt/mysql/bin/mysql -uroot -proot123 -e "show processlist" UAS|awk -F"\t" 'BEGIN{OFS="\t"};{print $8}'|sed -n '2,$p')"
	local info=""
	local index=1
	local oldIFS=$IFS
	#指定默认的分隔符为换行
	IFS="
"
	for i in $mysql_info; do
		if [[ "$i" != "NULL" && "$i" != "show processlist" ]]; then
			if [[ $index == 1 ]]; then
				#statements
				info="$i"
				index=$(expr $index + 1)
			else
				info=${info}"\n$i"
			fi
		fi
	done
	

	#这种方式理论是可以的，但是打印$info的时候，info中总有单引号，这个单引号会提前结束awk的action部分，所以换种方式
	#echo $(ps auxc|sed -n '2,$p'|sort -nr -k3|head -n $1|awk '{printf("%s   %s"),$11,$3;if($11=="mysqld") print "'$info'";else printf("\n");}')
	local topN_result="$(ps auxc|sed -n '2,$p'|sort -nr -k3|head -n $1|awk '{print $11,$3}')"
	for res in $topN_result; do
		echo $res|awk '{printf("%10s%7s"),$1,$2}'
		if [[ $res =~ ^mysqld ]]; then
			printf "    %s\n" $info
		else
			echo
		fi
	done
	IFS=$oldIFS
}

#计算当前系统的开机和运行时间
function online_time() {
	local online_second=$(cat /proc/uptime|awk '{print $1}'|awk -F"." '{print $1}')
	local online_min=0
	local online_hour=0
	local online_time="$online_second"
	local start_time=$(date -d "$online_second second ago" +"%Y-%m-%d %H:%M:%S")
	if (( $online_second > 60 )); then
		online_min=$(expr $online_second / 60)
		online_second=$(expr $online_second % 60)
		if (( $online_second < 10 )); then
			online_second="0$online_second"
		fi
		online_time="${online_min}:${online_second}"
	fi
	if (( $online_min > 60 )); then
		online_hour=$(expr $online_min / 60)
		online_min=$(expr $online_min % 60)
		if (( $online_hour < 10 )); then
			online_hour="0$online_hour"
		fi
		if (( $online_min < 10 )); then
			online_min="0$online_min"
		fi
		online_time="${online_hour}:${online_min}:${online_second}"
	fi
	printf "%-21s%16s%16s\n" "$(date "+%F %H:%M:%S")" "$start_time" "$online_time"
}

#display函数用来接收用户输入的命令，然后显示监控结果
function display() {
	if [[ ! $1 =~ [[:digit:]] && $1 != "--top-cpu" && $1 != "--help" && $1 != "-t" ]]; then
		echo 'you must give the time interval args'
		exit 1
	fi
	if [[ $1 =~ [[:digit:]] ]]; then
		if (( $1 <= 0 )); then
			echo "invalid args: $1"
			exit 1
		fi
	fi


	if [[ $1 == "--help" ]]; then
		echo "./sys interval [option] | [--top-cpu] n | [--help]"
		echo "除了查看top-cpu命令外的其他所有命令，必须在第一个参数提供一个监控的时间间隔。"
		echo "option选项如下："
		echo "	-t  	显示系统开机时间和已运行时间"
		echo "	-c n 	监控cpu当前的状态，n参数（可选）指定监控某个cpu核的状态，如：sys 1 -c 1 表示间隔1s内，cpu1的状态，如省略，默认查看cpu的状态"
		echo "	-p  	监控进程的状态"
		echo "	-m  	监控内存的状态"
		echo "	-s  	监控swap空间的状态"
		echo "	-n n 	监控网卡的实时流量，n参数（可选）指定监控某块网卡的流量，如：sys 1 -n 0 表示间隔1s内eth0网卡的流量，如省略，默认监控eth0"
		echo "	-l  	监控系统负载情况，显示1m  5m  15m内的负载"
		echo "	-g  	监控内存中页面置换的情况"
		echo "	-d hda|hdb|total 	监控磁盘IO的实时流量，后面的参数（可选）指定查看那一块磁盘，系统中有哪些磁盘可以通过fdisk -l查看，或者直接写total查看所有磁盘的总流量，如省略，默认为total"
		echo "	--top-cpu n 		查看系统中最占用cpu的topN进程，n参数（可选）指定前n个，如省略，n默认为5"
		echo "	--help	查看帮助信息"
		exit 1
	fi

	is_exist -t 1 $*
	local t_index=$(echo $?)
	if [[ $t_index != 0 ]]; then
		echo "-------date---------------start time----------online time-------"
		online_time
		exit 1
	fi

	#echo $(date "+%F %H:%M:%S")
	export LANG=zh_CN.UTF-8



	#判断是否有c选项(cpu)
	is_exist -c $*
	local c_index=$(echo $?)

	if [[  $c_index != 0 ]]; then
		# get_value $c_index $*
		# local c_value=$(echo $?)
		get_num_value $c_index $*
		local c_value=$return_value

		echo "--------total-cpu${c_value}-usage--------"
		echo " usr  sys   idl  wai  hiq  siq"
		echo " ---  ---  ----  ---  ---  ---"
		init_cpu $c_value
	fi

	#判断是否有-p选项(process----有点问题(计算目前正在running的进程计算有点问题))
	is_exist -p $*
	local p_index=$(echo $?)
	if [[ $p_index != 0 ]]; then
		echo "---procs---"
		echo "run blk new"
		echo "--- --- ---"
		init_process
	fi

	#判断是否有m选项---memory
	is_exist -m $*
	local m_index=$(echo $?)
	if [[ $m_index != 0 ]]; then
		echo "-----------memory-usage----------"
		echo "   used   buff     cach     free"
		echo " ------  -----   ------   ------"
		memory
	fi

	#判断是否有s选项----swap
	is_exist -s $*
	local s_index=$(echo $?)
	if [[ $s_index != 0 ]]; then
		echo "----------swap---------"
		echo "  used     free    cach"
		echo " -----    -----   -----"
		swap
	fi

	#判断是否有-n选项----监控网络实时流量
	is_exist -n $*
	local n_index=$(echo $?)
	if [[ $n_index != 0 ]]; then
		get_num_value $n_index $*
		local n_value=$return_value
		if [[ -z $n_value ]]; then
			n_value=0
		fi
		echo "------------eth${n_value}------------"
		echo "   recv   send   repa   sepa"
		echo "  -----  -----  -----  -----"
		if [[ -z $n_value ]]; then
			n_value=0
		fi
		init_network $n_value
	fi

	is_exist -l $*
	local l_index=$(echo $?)
	if [[ $l_index != 0 ]]; then
		echo "---load-avg---"
		echo " 1m   5m  15m "
		echo "---- ---- ----"
		load_avg
	fi

	#判断是否有-g选项，页面置换
	is_exist -g $*
	local g_index=$(echo $?)
	if [[ $g_index != 0 ]]; then
		echo "---paging--"
		echo "  in   out "
		echo "-----  ----"
		init_page
	fi

	#判断是否有-d选项，磁盘读写信息
	is_exist -d $*
	local d_index=$(echo $?)
	if [[ $d_index != 0 ]]; then
		get_str_value $d_index $*
		local d_value=$str_return_value
		if [[ -z $d_value ]]; then
			d_value="total"
		fi
		echo "--dsk/${d_value}--"
		echo "read  writ"
		echo "----  ----"
		if [[ $d_value != "total" ]]; then
			init_disk $d_value
		else
			init_total_disk		#如果是total参数的话，初始化所有磁盘总的读写信息
		fi
	fi

	#判断是否有--top-cpu参数，显示topN-cpu的进程
	is_exist --top-cpu 1 $*		#这里加一个1，是为了在形式上和其他参数保持一致，并不影响判断是否存在该选项的结果
	local top_index=$(echo $?)
	if [[ $top_index != 0 ]]; then
		#statements
		get_num_value $top_index 1 $*
		local top_value=$return_value
		if [[ -z $top_value ]]; then
			top_value=5
		fi
		echo "---process   %cpu    info---"
		echo "----------   ----    -------"
		topN_cpu $top_value
		exit 0
	fi

	turn_page=3
	while true
	do
		sleep $1
		#set -x
		if [[ $c_index != 0 ]]; then
			cpu $c_value
			#echo $LINES
			if [[ $turn_page == $LINES ]]; then
				echo "--------total-cpu${c_value}-usage--------"
				echo " usr  sys   idl  wai  hiq  siq"
				echo " ---  ---  ----  ---  ---  ---"
				turn_page=3
			else
				turn_page=$(expr $turn_page + 1)
			fi
		fi
		#set +x

		if [[ $p_index != 0 ]]; then
			process
		fi

		if [[ $m_index != 0 ]]; then
			memory
		fi

		if [[ $s_index != 0 ]]; then
			swap
		fi

		if [[ $n_index != 0 ]]; then
			network $n_value
		fi

		if [[ $l_index != 0 ]]; then
			#statements
			load_avg
		fi

		if [[ $g_index != 0 ]]; then
			#statements
			page
		fi

		if [[ $d_index != 0 ]]; then
			#statements
			if [[ $d_value != "total" ]]; then
				#statements
				disk_io $d_value
			else
				total_disk_io
			fi
			
		fi
	done
}

display $*

#export -f memory
#export -f swap
#export -f display
#watch -t -n $1 "display"

